package com.arcaneshift.servlet.action.game;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.arcaneshift.model.action.game.BattleEngine;
import com.arcaneshift.model.request.PlayerReadyRequest;
import com.arcaneshift.model.response.PlayerReadyResponse;
import com.arcaneshift.model.validators.GameValidator;
import com.arcaneshift.servlet.abstracts.AbstractGameServlet;
import com.arcaneshift.servlet.util.network.RequestAction;
import com.arcaneshift.servlet.util.parser.PlayerReadyXMLParser;
import com.arcaneshift.util.GameCacheContainer;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.validators.ActionValidator;
import com.common.security.abstracts.AbstractException;
import com.common.security.errors.ParserError;
import com.common.security.exceptions.ParserException;
import com.common.servlet.interfaces.IParser;

public class PlayerReadyServlet extends AbstractGameServlet {
	private static final long serialVersionUID = 1L;
	private static final Set<AbstractValidator> validators = new HashSet<AbstractValidator>();
	
	private PlayerReadyXMLParser parser = new PlayerReadyXMLParser();
	private PlayerReadyRequest requestContent;
	
	static {
		validators.add(new ActionValidator(RequestAction.PLAYER_READY));
		validators.add(new GameValidator());
	}

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) {
		try {
			/*
			if (game.playersReady()) {
				startGame(game);
				
				if (game.getPlayer1().getChannel() != null) {
					playerReadyResponse.setHand(game.getPlayer1HandCards());
					playerReadyResponse.setPlayer(1);
					sendChannelResponse(game.getPlayer1().getSession().getUserId(), playerReadyResponse);
				} else if (requestContent.getPlayer() == 1) {
					playerReadyResponse.setHand(game.getPlayer1HandCards());
					playerReadyResponse.setPlayer(1);
				}
				
				if (game.getPlayer2().getChannel() != null) {
					playerReadyResponse.setHand(game.getPlayer2HandCards());
					playerReadyResponse.setPlayer(2);
					sendChannelResponse(game.getPlayer2().getSession().getUserId(), playerReadyResponse);
				} else if (requestContent.getPlayer() == 2) {
					playerReadyResponse.setHand(game.getPlayer2HandCards());
					playerReadyResponse.setPlayer(2);
				}
			}
			*/
			
			BattleEngine game;
			do {
				game = GameCacheContainer.getGame(requestContent.getGameId());
				game.setPlayerReady(requestContent.getPlayer());
				
				if (game.playersReady()) {
					startGame(game);
				}
			} while (!GameCacheContainer.canSave(game.getId(), 0));
			sendPlayerActions(game);
			GameCacheContainer.updateGame(game);
			
			PlayerReadyResponse playerReadyResponse = new PlayerReadyResponse();
			playerReadyResponse.setRequestAction(requestContent.getAction());
			
			response.setContentType("text/xml");
			response.getWriter().write(parser().parseResponse(playerReadyResponse));
			response.getWriter().close();
		} catch (ParserException parserException) {
			exception(parserException, response);
		} catch (IOException ioException) {
			error(new ParserError(ParserError.STREAM_ERROR), response);
		}
	}
	
	private void startGame(BattleEngine game) {
		game.loadStartingHand();
		//game.startRounds();
	}
	
	@Override
	public void setRequest(AbstractRequest abstractRequest) {
		requestContent = (PlayerReadyRequest) abstractRequest;
	}

	@Override
	public Set<AbstractValidator> getValidators() {
		return validators;
	}

	@Override
	public IParser parser() {
		return parser;
	}
	
	@Override
	public void exception(AbstractException exception, HttpServletResponse response) {
		super.exception(exception, response);
		
		switch (exception.getCode()) {
			 case ParserException.CONVERSION_EXCEPTION: {
				 error(new ParserError(ParserError.VALUE_CONVERT_ERROR), response);
				 break;
			 }
		}
	}
}
